<template>
  <div id="staff">
    <el-table
      :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%"
    >
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="性别" prop="gender" />
      <el-table-column label="联系方式" prop="phone" />
      <el-table-column label="身份证" prop="card" />
      <el-table-column label="所属部门" prop="role" />
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" :data-index="scope.$index" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">Edit</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出表单—————————————————————————————————————— -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.card" />
        </el-form-item>
        <el-form-item label="所属部门">
          <el-select v-model="form.role" placeholder="请选择活动区域">
            <el-option label="维修部" value="shanghai" />
            <el-option label="业务部" value="beijing" />
            <el-option label="行政部" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="makeSure">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import staffApi from '../../api/staff.js'

export default {
  data() {
    return {
      tableData: [],
      search: '',
      dialogFormVisible: false,
      form: {
        name: '',
        phone: '',
        role: '',
        card: '',
        gender: ''
      }
    }
  },
  async mounted() {
    const data = await staffApi.getAll()
    const result = data.filter(item => item.role !== `admin`)
    this.tableData = result
  },
  methods: {
    // 编辑
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    // 确认修改
    async makeSure() {
      await staffApi.updata(this.form)
      this.tableData = await staffApi.getAll()
      this.dialogFormVisible = false
    },

    // 删除
    async handleDelete(row) {
      const { _id } = row
      await staffApi.deleteOne(_id) // 删除
      this.tableData = await staffApi.getAll()
      this.$message({
        message: '删除成功',
        type: 'success'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
#staff {
  width: 90%;
  margin: auto;
  margin-top: 30px;
}
</style>
